
import pytest
from selenium import webdriver

driver_ = None

@pytest.fixture(scope="session")
def browser():
    global driver_
    driver = webdriver.Chrome()
    driver_ = driver
    driver.maximize_window()
    driver.delete_all_cookies()
    yield driver
    driver.quit()

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
    pytest_html = item.config.pluginmanager.getplugin('html')
    outcome = yield
    report = outcome.get_result()
    extra = getattr(report, 'extra', [])
    if report.when == 'call':
        xfail = hasattr(report, 'wasxfail')
        if (report.skipped and xfail) or (report.failed and not xfail):
            screen = driver_.get_screenshot_as_base64() # 截图
            extra.append(pytest_html.extras.png(screen))
            extra.append(pytest_html.extras.html('<div>Additional HTML</div>'))
        report.extra = extra